<?php
$argl = array_merge($argl,array(
	"thread" => array(
		"required"	=> 0,
		"hasvalue"	=> 1,
		"desc"		=> "Target thread(s) (if needed), divided with commas",
	),
	"sleep" => array(
		"required"	=> 0,
		"hasvalue"	=> 1,
		"default"	=> 6000,
		"desc"		=> "Sleep this many centiseconds between posts.",
	),
	"post-limit" => array(
		"required"	=> 0,
		"hasvalue"	=> 1,
		"default"	=> 200,
		"desc"		=> "Limit after which wipe stops. (Per thread if thread is specified)",
	),
	"auto-delete" => array(
		"required"	=> 0,
		"hasvalue"	=> 1,
		"default"	=> 1,
		"desc"		=> "Auto-delete wipe posts when limit reached (Does NOT work with thread-wiping).",
	),
	"stats" => array(
		"required"	=> 0,
		"hasvalue"	=> 1,
		"default"	=> 500,
		"desc"		=> "Print stats interval in centiseconds.",
	),
	"alter" => array(
		"required"	=> 1,
		"hasvalue"	=> 1,
		"default"	=> "quick",
		"desc"		=> "Image altering method to use. Avaiable : quick,stealth,string",
	),
	"stealth" => array(
		"required"	=> 1,
		"hasvalue"	=> 1,
		"default"	=> 15,
		"desc"		=> "Number of dots to alter with stealth method",
	),
	"min-size" => array(
		"required"	=> 0,
		"hasvalue"	=> 1,
		"default"	=> 0,
		"desc"		=> "Resize pic to this value if it is smaller than specified.",
	),
	"max-size" => array(
		"required"	=> 0,
		"hasvalue"	=> 1,
		"default"	=> 200,
		"desc"		=> "Resize pic to this value if it is bigger than specified.",
	),
	"jpg-quality" => array(
		"required"	=> 0,
		"hasvalue"	=> 1,
		"default"	=> 60,
		"desc"		=> "JPEG picture quality. Ranges from 0 to 100.",
	),	
));
function wipeMain($TID,$p)
{
	global $Proxies,$threads,$threadsR,$args,$MTID,$Stats,$PostedThreads,$ProxiesAlive,$FlagLimitReached,$FailedCaptchas;
	if (!isset($p['task']))
	{
		$MTID = $TID;
		$threads = array();
		$FlagLimitReached = 0;
		$FailedCaptchas=0;
		$PostedThreads = array();
		if (isset($args['thread']))
		{
			$threads = explode(",",$args['thread']);
		}
		$Stats['Posted']=0;
		$Stats['Errors']=0;
		$Stats['TotalPosted']=0;
		$Stats['TotalErrors']=0;		
		if (count($threads))
		{
			foreach($threads as $ti => $tid)
			{
				$Stats['Threads'][$tid]['Posted']=0;
				$Stats['Threads'][$tid]['TotalPosted']=0;
				$threadsR[$tid]=$ti;
			}
		}		
		foreach($Proxies as $i => $proxy)
		{
			AddThread("Wipe $i",'wipeThread',array());
		}
		if ($args['stats'])
		{
			QueueThread($TID,GetMicroTime()+$args['stats'],'wipeMain',array("task"=>"stats"));
		}
	}
	else if($p['task'] == 'stats')
	{
		EchoMsg("Posted $Stats[Posted]/$Stats[TotalPosted]\tHTTP Errors $Stats[Errors]/$Stats[TotalErrors]\tAlive Proxies $ProxiesAlive\tFailed Captcas $FailedCaptchas");
		$Stats['Posted']=0;
		$Stats['Errors']=0;
		if (count($threads))
		{
			foreach($threads as $ti => $tid)
			{
				EchoMsg("Thread $tid Posted {$Stats['Threads'][$tid]['Posted']}/{$Stats['Threads'][$tid]['TotalPosted']}");
				$Stats['Threads'][$tid]['Posted']=0;
			}
		}
		if (!$FlagLimitReached OR count($threads))
		QueueThread($TID,GetMicroTime()+$args['stats'],'wipeMain',array("task"=>"stats"));
	}
	else if($p['task'] == 'posted')
	{
		$Stats['Posted']++;
		$Stats['TotalPosted']++;
		if (!count($threads))
		{
			$PostedThreads[$p['thread']]=$p['password'];
			if ($args['post-limit'] AND $Stats['TotalPosted'] >= $args['post-limit'])
			{
				if (!$FlagLimitReached)
				{
					$FlagLimitReached = 1;
					EchoMsg("Post Limit reached, halting posting.");
					if ($args['auto-delete'])
						QueueThread($TID,GetMicroTime()+100,'wipeMain',array("task"=>"delete"));
				}
				else
				{
					AddThread("Delete",'DeletePost',array('task'=>'init','tids'=>array(0=>$p['thread']),'pass'=>$p['password'],'callbackt'=>$TID));
				}
			}
		}
		else
		{
			$Stats['Threads'][$p['thread']]['Posted']++;
			$Stats['Threads'][$p['thread']]['TotalPosted']++;
			if ($args['post-limit'] AND $Stats['Threads'][$p['thread']]['TotalPosted'] >= $args['post-limit'])
			{
				unset($threads[$threadsR[$p['thread']]]);
				EchoMsg("Post limit for thread $p[thread] reached.");
				if (!count($threads))
				{
					$FlagLimitReached = 1;
					EchoMsg("Post Limit reached, halting posting.");
				}
			}
		}
	}
	else if($p['task'] == 'delete')
	{
		EchoMsg("Deleting wipe posts.");
		if ($args['random-password'])
		{
			foreach($PostedThreads as $tid => $pass)
			{
				AddThread("Delete",'DeletePost',array('task'=>'init','tids'=>array(0=>$tid),'pass'=>$pass,'callbackt'=>$TID));
			}
		}
		else
		{
			$tids=array();
			foreach($PostedThreads as $tid => $pass)
			{
				$tids[]=$tid;
			}
			AddThread("Delete",'DeletePost',array('task'=>'init','tids'=>$tids,'pass'=>$args['password'],'callbackt'=>$TID));
		}
	}
	else if($p['task'] == 'deleted')
	{
		EchoMsg("Deleted wipe posts.");
	}
	else if($p['task'] == 'error')
	{
		$Stats['Errors']++;
		$Stats['TotalErrors']++;
	}
	else if($p['task'] == 'banned')
	{
		
	}
	else if ($p['task'] == 'dead')
	{
		
	}
}
function wipeThread($TID,$p)
{
	global $threads,$Threads,$ThreadProxy,$ThreadID,$PostSets,$args,$proxy,$MTID,$FlagLimitReached;
	if (!isset($Threads[$TID]['proxy']))
	{
		ProxyGet();
	}
	if ($p['task'] != 'done' AND $p['task'] != 'cerror')
	{
		if (!$FlagLimitReached OR count($threads))
		{
			if (count($threads))
			{
				$thread = $threads[array_rand($threads)];
			}
			else
			{
				$thread = 0;
			}
			
			$setn = array_rand($PostSets);
			$set  = &$PostSets[$setn];
			
			if ($args['no-pic'] OR !count($set['pics']))
			{
				$pic = '';
			}
			else
			{
				$pic = MakePic($set['path'].'/'.$set['pics'][array_rand($set['pics'])]);
			}
			
			if ($args['silent'] OR !count($set['text']))
			{
				$text = "";
			}
			else
			{
				$text = $set['text'][array_rand($set['text'])];
				//$text = str_replace(array('$TID$','$RAND$'),array($tid,$bid),$text);
			}
			
			/*
			if ($args['cites'])
			{
				$cite = GetCite($args['cites']);
				EchoMsg("Cite from $args[cites] : \r\n$cite");
				$txt = str_replace("\$CITE\$",$cite,$txt);
			}
			*/
			
			if ($args['random-password'])
			{
				$pass = GetRandomString(8);
			}
			else
			{
				$pass = $args['password'];
			}
			CallThread("MakePost",array(
				"thread"=> $thread,
				"pic"	=> $pic,
				"text"	=> $text,
				"password"=> $pass,
				"captcha"=>$p['captcha'],
				"proxy"	=> $proxy,
				"cookies"=>array(),
			));
		}
		else
		{
			ProxyRelease();
			RemoveThread($TID);
		}
	}
	else
	{
		if ($p['pic'])
			unlink($p['pic']);
		EchoMsgDebug("wipeThread() task=done, result=$p[result] thread=$p[thread] proxy=$proxy[proxy]",2);
		if ($p['result'] == PostResult_OK)
		{
			ProxyReportOK();
			QueueThread($MTID,0,'wipeMain',array("task"=>"posted","proxy"=>$proxy,"thread"=>$p['thread'],'password'=>$p['password']));
			QueueThread($TID,GetMicroTime()+$args['sleep'],'wipeThread',array());
		}
		else if ($p['result'] == PostResult_Banned)
		{
			if(ProxyReportBan())
			{
				QueueThread($MTID,0,'wipeMain',array("task"=>"banned","proxy"=>$proxy));
			}
			else
			{
				QueueThread($TID,0,'wipeThread',array());
			}
		}
		else if ($p['result'] == PostResult_Timeout OR $p['result'] == PostResult_HTTPErr OR $p['result'] == PostResult_UnknownErr)
		{
			if(ProxyReportError())
			{
				QueueThread($MTID,0,'wipeMain',array("task"=>"dead","proxy"=>$proxy));
			}
			else
			{
				QueueThread($MTID,0,'wipeMain',array("task"=>"error","proxy"=>$proxy));
				QueueThread($TID,0,'wipeThread',array('captcha'=>$p['captcha']));
			}			
		}
		else
		{
			QueueThread($TID,0,'wipeThread',array());
		}
	}
}
?>